//
//  ChatModel.swift
//  Exchange
//
//  Created by rui on 2018/6/21.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

struct ChatReq {
    enum CmdType: String {
        ///登陆
        case login = "login"
        ///订单历史消息
        case orderHistory = "orderHistoryMsg"
        ///订单消息
        case order = "userOrderMsg"
        ///广告历史消息
        case adHistory = "mutualHistoryMsg"
        ///广告消息
        case ad = "mutualMsg"
    }
    
    public static let separated = "#@%&$*!~"

    var cmd: ChatReq.CmdType
    var receiver: Int?
    var advId: Int?
    var orderId: Int?
    var msg: String?
    
    var waitOrderId: Int?
    
    init(cmd: ChatReq.CmdType) {
        self.cmd = cmd
    }
    
    var req: String? {
        guard let userId = AppInfo.default.userId else {
//            CYMBShow.showMessage("你还未登录，请先登录")
            return nil
        }
        let uuid = Int(Date().timeIntervalSince1970 * 1000)
        var content:[String:Any] = ["method":self.cmd.rawValue,"userId":userId]
        var data: [String:Any] = [:]
        if cmd == .orderHistory {
            data.updateValue(self.orderId ?? 0, forKey: "orderId")
        }else if cmd == .order {
            data.updateValue(self.orderId ?? 0, forKey: "orderId")
            if let msg = self.msg {
                data.updateValue(msg, forKey: "msg")
            }
        }else if cmd == .ad {
            data.updateValue(self.advId ?? 0, forKey: "advId")
            data.updateValue(self.waitOrderId ?? 0, forKey: "waitOrderId")
            if let msg = self.msg {
                data.updateValue(msg, forKey: "msg")
            }
            data.updateValue(self.receiver ?? 0, forKey: "receiver")
        }else if cmd == .adHistory {
            data.updateValue(self.waitOrderId ?? 0, forKey: "waitOrderId")
            data.updateValue(self.advId ?? 0, forKey: "advId")
            data.updateValue(self.receiver ?? 0, forKey: "receiver")
        }
        content.updateValue(data, forKey: "data")
        
        let reqObj:[String:Any] = ["cmd":"c2c_chat","content":content]
        
        return reqObj.jsonString ?? ""
    }
}
